/**
 * 常用JS变量:
 * agentEvent = 代理模式下自动点击模块
 * acEvent= 无障碍模式下自动点击模块
 * device = 设备信息模块
 * file = 文件处理模块
 * http = HTTP网络请求模块
 * shell = shell命令模块
 * thread= 多线程模块
 * image = 图色查找模块
 * utils= 工具类模块
 * global = 全局快捷方式模块
 * 常用java变量：
 *  context : Android的Context对象
 *  javaLoader : java的类加载器对象
 * 导入Java类或者包：
 *  importClass(类名) = 导入java类
 *      例如: importClass(java.io.File) 导入java的 File 类
 *  importPackage(包名) =导入java包名下的所有类
 *      例如: importPackage(java.util) 导入java.util下的类
 *
 */

function main() {
    let odds = 1;
    let password = "868082bB";
    //开始再这里编写代码了！！
    utils.openApp("vip.mytokenpocket")
    let learnMore = text("了解更多")
    click(learnMore);
    sleep(2000);
    let val = text("资产");
    if(!waitExistNode(val,20000)){
        let backHome =  id("vip.mytokenpocket:id/iv_back");
        click(backHome);
    }
    let trx = id("vip.mytokenpocket:id/token_icon");
    let trxWait = waitExistNode(trx,20000);
    if(!trxWait){
        toast("找不到TRX");
        return;
    }
    let result = click(trx);
    toast("TRX点击"+result);
    let inText = text("转入");
    let inTextWait = waitExistNode(inText,10000);
    if(!inTextWait){
        toast("找不到转入");
        return;
    }
    let inTextClick = click(inText);
    toast("转入点击"+inTextClick);
    let record= id("vip.mytokenpocket:id/rv_record");
    waitExistNode(record,10000);
    let listNode= id("vip.mytokenpocket:id/rv_record").getOneNodeInfo(10000);
    console.log("ss"+listNode);
    let itemList = listNode.allChildren();
    console.log(itemList.length);
    for (let i=0;i<itemList.length;i++){
        let titleToken = text("Token详情")
        let  titleExist = waitExistNode(titleToken,5000);
        if(!titleExist){
            console.log(("详情页错误"))
            main();
            return;
        }
        sleep(1000);

        waitExistNode(text("转入"),10000);
        let item = itemList[i];
        console.log("index:"+item+"_"+i);

        let itemClick = item.click();
        console.log(itemClick)

        let inAccountText = id("vip.mytokenpocket:id/tv_address");
        let inAccountWait = waitExistNode(inAccountText,10000);
        if(!inAccountWait){
            toast("没有找到账号");
            continue;
        }
        let inaccount = getText(inAccountText)[0];
        toast("复制账号"+inaccount);

        let hashText = id("vip.mytokenpocket:id/tv_transaction_id");
        let hasTextWait = waitExistNode(hashText,10000);
        if(!hasTextWait){
            toast("没有hash");
            continue;
        }
        let hash = getText(hashText);
        console.log("hash值"+hash);

        let inMoneyText = id("vip.mytokenpocket:id/tv_amount");
        let inMoneyWait = waitExistNode(inMoneyText,10000);
        if(!inMoneyWait){
            toast("没有转让金额");
            continue;
        }
        let inMoney = getText(inMoneyText);
        console.log("inMoney "+inMoney);
        inMoney = inMoney.toString().split(" TRX")[0].slice(1);
        console.log("inMoney:"+inMoney)
        let back = id("vip.mytokenpocket:id/iv_back");
        waitExistNode(back,10000);
        let backClick = click(back);
        console.log("back :"+backClick);

        let outMoney = getTransferMoney(hash,inMoney,odds);
        if(outMoney===0){
            toast("单双号不正确，无需转账");
            continue;
        }

        var transfer = id("vip.mytokenpocket:id/tv_transfer");
        let transferWait = waitExistNode(transfer,10000);
        if(!transferWait){
            toast("没有转账按钮");
            continue;
        }
        let transeferClick = click(transfer);
        toast("点击转账"+transeferClick);

        let directTransfer = text("直接转账");
        let directTransferWait = waitExistNode(directTransfer,10000);
        if(!directTransferWait){
            toast("没有直接转账按钮");
            continue;
        }
        click(directTransfer);

        var transferAccountText = id("vip.mytokenpocket:id/et_receiver");
        let transferAccountTextWait = waitExistNode(transferAccountText,10000);
        if(!transferAccountTextWait){
            toast("没有直接转账按钮");
            continue;
        }
        inputText(transferAccountText,inaccount);

        var transferMoneyText = id("vip.mytokenpocket:id/edt_amount");
        let transferMoneyTextWait = waitExistNode(transferMoneyText,10000);
        if(!transferMoneyTextWait){
            console.log("error not found transferMoney");
            continue;
        }
        inputText(transferMoneyText,outMoney);
        console.log("转出金额"+outMoney);
        var confirmBtn = id("vip.mytokenpocket:id/tv_next");
        let confirmBtnWait = waitExistNode(confirmBtn,10000);
        if(!confirmBtnWait){
            console.log("error not found confiremButton");
            continue;
        }
        click(confirmBtn);

        let continueTransfer = id("vip.mytokenpocket:id/tv_confirm");
        let continueTransferWait = waitExistNode(continueTransfer,10000);
        if(!continueTransferWait){
            console.log("error not found continue button");
            continue;
        }
        click(continueTransfer);

        let pay = id("vip.mytokenpocket:id/tv_confirm");
        let payWait = waitExistNode(pay,10000);
        if(!payWait){
            console.log("error not found pay");
            continue;
        }
        click(pay);

        let pwd = id("vip.mytokenpocket:id/edt_dialog_pwd");
        let pwdWait = waitExistNode(pwd,10000);
        if(!pwdWait){
            console.log("error not found pay");
            continue;
        }
        inputText(pwd,password);

        let confirmPay = id("vip.mytokenpocket:id/tv_ok");
        let confirmPayWait = waitExistNode(confirmPay,10000);
        if(!confirmPayWait){
            console.log("error not found pay");
            continue;
        }
        console.log("点击确认按钮");
        click(confirmPay);
    }
}

function getTransferMoney(hash,inMoney,odds){
    let hashMoney = getHashNumber(hash);
    if(hashMoney===-1){
        return 0;
    }
    console.log("hash last:"+hashMoney);
    let inMoneyIsEvenNumber =parseInt(inMoney.toString().slice(-1))%2;
    let hashMoneyIsEvenNumber = hashMoney%2;
    console.log("inMoneyIsEvenNumber :"+inMoneyIsEvenNumber+":hashMoneyIsEvenNumber:"+hashMoneyIsEvenNumber);
    if(inMoneyIsEvenNumber===hashMoneyIsEvenNumber){
        return odds*inMoney;
    }
    return 0;
}

function getHashNumber(hashVal){
    let lastNumber = hashVal.toString().slice(-1);
    console.log(hashVal+"> last number:"+hashVal.toString().slice(-1));
    if(isNumeric(lastNumber)){
        return lastNumber;
    }else{
        let secondNumber = hashVal.toString().slice(hashVal.length-2,hashVal.length-1);
        console.log("second number:"+secondNumber);
        if(isNumeric(secondNumber)){
            return secondNumber;
        }else{
            return -1;
        }
    }
}

function isNumeric(str) {
    return !isNaN(parseInt(str)) || !isNaN(parseFloat(str));
}


main();
